package com.hd.platform.server.api.bussiness.impl;

import com.hd.model.Pic;
import com.hd.platform.server.api.bussiness.SimlarImageService;
import com.hd.platform.util.image.ImageIndexInit;
import com.hd.platform.util.image.ImagePHash;
import com.hd.platform.util.image.ImgNode;
import com.hd.platform.util.image.PhashIndex;
import net.semanticmetadata.lire.HDLireOper;
import org.springframework.stereotype.Service;


import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by qingshengzheng on 17/5/6.
 */
@Service("SimlarImageService")
public class SimlarImageServiceImpl implements SimlarImageService {
    private PhashIndex phashIndex = ImageIndexInit.getPhashIndex();
    private ImagePHash imagePhash = ImagePHash.getInstance();
    private HDLireOper lireOper = new HDLireOper();

    /**
     * 根据用户上传的图片或者相似的图片
     *
     * @param file
     * @return
     */

    @Override
    public List<Pic> getSimlarPics(File file) {
        String phash = "";
        BufferedImage img = null;
        try {
            img = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Pic> v = new ArrayList<Pic>();
        try {
            v = lireOper.searchSimlarImages(img);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return v;
    }

    @Override
    public List<Pic> getSimlarPics(BufferedImage image) {
        List<Pic> v = new ArrayList<Pic>();
        try {
            v = lireOper.searchSimlarImages(image);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return v;
    }


    @Override
    public void uploadFile(File file) {
        try {
            String phash = imagePhash.getHash(file);
            String path = "";
            String filename = "";

            ImgNode imgNode = new ImgNode(path, filename, phash);
            phashIndex.add(imgNode);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
